<template>
  <div>
    <div style="height: 1000px">
      <FormPanel
        ref="formPanel"
        :cols="2"
        :labelWidth="110"
        :list="formList"
        :formType="type"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
        style="margin-top: 20px"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import {
  saveComOrgList,
  editSaveComOrgList,
  getComOrgTreeList } from '@/api/common/org/companyOrgManage';
import { dictionary } from '@/utils/dictMixin';
import { getJsonTree } from '@/utils/index';

export default {
  name: 'OrgPanel',
  mixins: [dictionary],
  components: {},
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      visible: false,
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.inputTree();
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.orgId = this.rowData.orgId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '组织代码',
          disabled: this.type=='edit'?true:false,
          fieldName: 'orgCode',
          placeholder: '请输入',
          initialValue: '',
          rules: [{ required: true, message: '请输入组织代码', trigger: 'blur' }, { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '组织名称',
          fieldName: 'orgName',
          placeholder: '请输入',
          initialValue: '',
          rules: [{ required: true, message: '请输入组织名称', trigger: 'blur' }, { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }]
        },
        {
          type: 'INPUT_TREE',
          label: '上级部门',
          disabled: this.type=='edit'?true:false,
          fieldName: 'parentOrgId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择上级组织', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '组织状态',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1001')
        },
        {
          type: 'SELECT',
          label: '组织类型',
          disabled: this.type=='edit'?true:false,
          fieldName: 'orgType',
          placeholder: '请选择',
          itemList: this.createDictList('1680')
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          placeholder: '备注',
          initialValue: ''
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await saveComOrgList(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editSaveComOrgList(this.saveData, this.orgId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(type) {
      this.$emit('close', false, type);
    },
    async inputTree() {
      const res = await getComOrgTreeList();
      var that = res.data
      that.forEach(el => (el.text = el.text));
      that.forEach(el => (el.value = el.orgId));
      that.forEach(el => (el.parentId = el.parent));
      // console.log('编辑部门树', getJsonTree(that));
      this.formList[2].itemList = getJsonTree(that);
    }
  }

};
</script>
